package com.willpower.eureka.auth;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @author: wen-yi;
 * @date: 2021/10/31 17:01;
 * @Description: 自定义规则权限控制
 */
@Service
public class AccessServer {
    public boolean hasPermission(HttpServletRequest request, Authentication authentication){
        String uri = request.getRequestURI();
        UserDetails principal = (UserDetails)authentication.getPrincipal();
        Collection<? extends GrantedAuthority> authorities = principal.getAuthorities();
        return authorities.contains(new SimpleGrantedAuthority(uri));
    }
}
